import random
import time

from dao.iotdbhelper import Iot
from dao.mqtthelp_v3 import MQTTClient
from dao.mysqlhelper import MySQLHelper


def search_dev(product_key):
    # 查询产品下的所有设备
    sql=f"select device_name from iot_device where product_key='{product_key}' and tenant_id='T0103' and status=1"
    mysql=MySQLHelper()
    dev_list=mysql.select(sql)
    print(dev_list)
    return dev_list

# 构造单个产品下所有设备的sql
def creat_sql(product_key,flag=0):
    # 构造iot  sql
    sql_list=[]
    dev_list=search_dev(product_key)
    recent_24h_time=int(time.time()*1000)-(random.randint(0, 24) * 3600 * 1000)
    if product_key=='q3Dh5ShgS':  # CO设备
        for dev in dev_list:
            num1=random.randint(0,100)
            num2=random.randint(0,100)
            if flag==1:
                sql=f'INSERT INTO root.iotdata.{product_key}.{dev[0]}(time,ct,VI_Data,v,nt,CO_Data) VALUES ({recent_24h_time},1715932080000,{num1},1,1715932080000,{num2})'
            else:
                sql=f'INSERT INTO root.iotdata.{product_key}.{dev[0]}(ct,VI_Data,v,nt,CO_Data) VALUES (1715932080000,{num1},1,1715932080000,{num2})'
            sql_list.append(sql)
    elif product_key=='o3wo5cogc': # 温湿度计
        for dev in dev_list:
            num1=random.randint(0,100)
            num2=random.randint(0,100)
            if flag==1:
                sql=f'INSERT INTO root.iotdata.{product_key}.{dev[0]}(time,ct,SD_Data,v,nt,WD_Data) VALUES ({recent_24h_time},1715932080000,{num1},1,1715932080000,{num2})'
            else:
                sql=f'INSERT INTO root.iotdata.{product_key}.{dev[0]}(ct,SD_Data,v,nt,WD_Data) VALUES (1715932080000,{num1},1,1715932080000,{num2})'
            sql_list.append(sql)
    elif product_key=='R3r25U2gU': # 泵房液位计
        for dev in dev_list:
            num3=round(random.uniform(0,2),4)
            if flag==1:
                sql=f'INSERT INTO root.iotdata.{product_key}.{dev[0]}(time,ct,YW_Data,v,nt) VALUES ({recent_24h_time},1715932080000,{num3},1,1715932080000)'
            else:
                sql=f'INSERT INTO root.iotdata.{product_key}.{dev[0]}(ct,YW_Data,v,nt) VALUES (1715932080000,{num3},1,1715932080000)'
            sql_list.append(sql)
    elif product_key=='m3Tp5MpgM':  # 照度仪
        for dev in dev_list:
            num4=round(random.uniform(400,600),4)
            if flag==1:
                sql=f'INSERT INTO root.iotdata.{product_key}.{dev[0]}(time,ct,ZD_Data,v,nt) VALUES ({recent_24h_time},1715932080000,{num4},1,1715932080000)'
            else:
                sql=f'INSERT INTO root.iotdata.{product_key}.{dev[0]}(ct,ZD_Data,v,nt) VALUES (1715932080000,{num4},1,1715932080000)'
            sql_list.append(sql)
    elif product_key=='a3QJ5UJgU':  # 亮度仪
        for dev in dev_list:
            num5=round(random.uniform(500,2000),4)
            if flag==1:
                sql=f'INSERT INTO root.iotdata.{product_key}.{dev[0]}(time,ct,LD_Data,v,nt) VALUES ({recent_24h_time},1715932080000,{num5},1,1715932080000)'
            else:
                sql=f'INSERT INTO root.iotdata.{product_key}.{dev[0]}(ct,LD_Data,v,nt) VALUES (1715932080000,{num5},1,1715932080000)'
            sql_list.append(sql)
    return sql_list

# 构造单个产品下所有设备的topic
def creat_topic(product_key):
    dev_list=search_dev(product_key)
    current_timestamp=int(time.time()*1000)
    topic_payload_list=[]
    if product_key=='q3Dh5ShgS':  # CO设备
        for dev in dev_list:
            num1=random.randint(0,100)
            num2=random.randint(0,100)
            topic=f'tlink/{product_key}/{dev[0]}/event/propertyReport/post'
            payload={
                "reqid": "248826154956B17794B53417ACC631CE",
                "v": 1,
                "t": current_timestamp,
                "method": "event.propertyReport.post",
                "spec": {
                "ack": 0
            },
            "data": {
                "VI_Data": {
                    "value": num1
                },
                "CO_Data": {
                    "value": num2
                }
            }
            }
            data=(topic,payload)
            topic_payload_list.append(data)
    if product_key=='o3wo5cogc': # 温湿度计
        for dev in dev_list:
            num1=random.randint(0,100)
            num2=random.randint(0,100)
            topic=f'tlink/{product_key}/{dev[0]}/event/propertyReport/post'
            payload={
                "reqid": "248826154956B17794B53417ACC631CE",
                "v": 1,
                "t": current_timestamp,
                "method": "event.propertyReport.post",
                "spec": {
                    "ack": 0
                },
                "data": {
                    "SD_Data": {
                        "value": num1
                    },
                    "WD_Data": {
                        "value": num2
                    }
                }
            }
            data=(topic,payload)
            topic_payload_list.append(data)
    if product_key=='R3r25U2gU': # 泵房液位计
        for dev in dev_list:
            num3=round(random.uniform(0,2),4)
            topic=f'tlink/{product_key}/{dev[0]}/event/propertyReport/post'
            payload={
                "reqid": "248826154956B17794B53417ACC631CE",
                "v": 1,
                "t": current_timestamp,
                "method": "event.propertyReport.post",
                "spec": {
                    "ack": 0
                },
                "data": {
                    "YW_Data": {
                        "value": num3
                    }
                }
            }
            data=(topic,payload)
            topic_payload_list.append(data)
    if product_key=='m3Tp5MpgM':  # 照度仪
        for dev in dev_list:
            num4=round(random.uniform(400,600),4)
            topic=f'tlink/{product_key}/{dev[0]}/event/propertyReport/post'
            payload={
                "reqid": "248826154956B17794B53417ACC631CE",
                "v": 1,
                "t": current_timestamp,
                "method": "event.propertyReport.post",
                "spec": {
                    "ack": 0
                },
                "data": {
                    "ZD_Data": {
                        "value": num4
                    }
                }
            }
            data=(topic,payload)
            topic_payload_list.append(data)
    if product_key=='a3QJ5UJgU':  # 亮度仪
        for dev in dev_list:
            num5=round(random.uniform(500,2000),4)
            topic=f'tlink/{product_key}/{dev[0]}/event/propertyReport/post'
            payload={
                "reqid": "248826154956B17794B53417ACC631CE",
                "v": 1,
                "t": current_timestamp,
                "method": "event.propertyReport.post",
                "spec": {
                    "ack": 0
                },
                "data": {
                    "LD_Data": {
                        "value": num5
                    }
                }
            }
            data=(topic,payload)
            topic_payload_list.append(data)
    return topic_payload_list

# 执行单个产品下所有设备的sql
def do_sql(product_key,num=1,flag=0):
    for i in range(num):
        sql_list=creat_sql(product_key,flag)
        for sql in sql_list:
            iot.non_search(sql)

# 串行  直接插数据库
def execute_task(product_list):
    for product in product_list:
        do_sql(product)     # num=1默认为1条数据，flag=0默认是一条最新的数据;   flag=1是最近24小时的数据,如果选最近24小时数据，建议num适当大点

#  CO设备 q3Dh5ShgS  温湿度计 o3wo5cogc  泵房液位计 R3r25U2gU  照度仪 m3Tp5MpgM 亮度仪 a3QJ5UJgU
product_list=['q3Dh5ShgS','o3wo5cogc','R3r25U2gU','m3Tp5MpgM','a3QJ5UJgU']

iot=Iot()
while True:
    execute_task(product_list)
    time.sleep(5)  # 每隔 5 秒执行一次任务

# 并行  直接插数据库
# iot=Iot()
# sql_lists=[]
# for product in product_list:
#     sql_list=creat_sql(product)     # num=1默认为1条数据，flag=0默认是一条最新的数据;   flag=1是最近24小时的数据,如果选最近24小时数据，建议num适当大点
#     sql_lists+=sql_list
# sql_lists+=sql_lists
# print(sql_lists)
# iot.concurrent_non_search(sql_lists)


# 通过mq插数据
# mqtt=MQTTClient()
# all_topic_payload_list=[]
# for product in product_list:
#     topic_payload_list=creat_topic(product)
#     all_topic_payload_list+=topic_payload_list
# # print(all_topic_payload_list)
# mqtt.concurrent_publish2(all_topic_payload_list)